package com.twm.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.twm.drug.common.ResultMapUtils;
import com.twm.drug.entity.Supplier;
import com.twm.drug.service.ISupplierService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 供应商相关的controller
 */
@Controller
@RequestMapping(value = "/supplier") //给一个公共的访问地址
public class SupplierController {
    @Autowired
    private ISupplierService iSupplierService;
    /**
     * 转向供应商页面
     */
    @RequestMapping
    public String supplier(){
        return "/supplier";
    }

    /**
     * 分页查询供应商列表
     * @param param
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/supplierQueryPages")
    @ResponseBody
    public Object supplierQueryPages(String param,
                                     @RequestParam(defaultValue = "1")int pageNum,  //为第1页
                                     @RequestParam(defaultValue = "10")int pageSize){ //每页数量为10条数据
        try {
            IPage<Supplier> iPage = iSupplierService.selectSupplierPage(pageNum,pageSize,param);
            return ResultMapUtils.getHashMapMysqlPage(iPage);
        }catch (Exception e){
            return ResultMapUtils.hashMapException(e);
        }
    }

    /**
     * 转向供应商新增页面
     * @return
     */
    @RequestMapping(value = "/supplierPage")
    public String supplierPage(){
        return "/supplierPage";
    }

    /**
     * 转向供应商编辑页面
     * 根据id查询到供应商，然后放入到model
     * @return
     */
    @RequestMapping(value = "/supplierQueryById")
    public String supplierQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        // 获取当前对象
        Supplier supplier = iSupplierService.querySupplierById(id);
        // 放入到model
        model.addAttribute("obj",supplier);
        return "/supplierPage";
    }

    /**
     * 添加一个供应商
     * 需要从前端返回supplier对象
     */
    @RequestMapping(value = "/supplierAdd")
    @ResponseBody
    public Object supplierAdd(Supplier supplier){
        try{
            // 设置当前时间
            supplier.setCreatetime(new Date());
            // 添加数据
            int i = iSupplierService.addSupplier(supplier);
            // 返回成功为1 不成功为0
            return ResultMapUtils.getHashMapSave(i);
        }catch (Exception e){
            return ResultMapUtils.hashMapException(e);
        }
    }
    /**
     * 修改一个供应商
     * 需要从前端返回supplier对象
     */
    @RequestMapping(value = "/supplierEdit")
    @ResponseBody
    public Object supplierEdit(Supplier supplier){
        try{
            // 添加数据
            int i = iSupplierService.editSupplier(supplier);
            // 返回成功为1 不成功为0
            return ResultMapUtils.getHashMapSave(i);
        }catch (Exception e){
            return ResultMapUtils.hashMapException(e);
        }
    }

    /**
     * 删除一个供应商
     * 需要从前端返回supplier对象
     */
    @RequestMapping(value = "/supplierDelById")
    @ResponseBody
    public Object supplierDelById(Integer id){
        try{
            // 添加数据
            int i = iSupplierService.delSupplierById(id);
            // 返回成功为1 不成功为0
            return ResultMapUtils.getHashMapDel(i);
        }catch (Exception e){
            return ResultMapUtils.hashMapException(e);
        }
    }

    /**
     * 获取所有供应商
     */
    @RequestMapping(value = "/supplierList")
    @ResponseBody
    public Object supplierList(){
        List<Supplier> supplierList = iSupplierService.querySupplierList();
        return ResultMapUtils.getHashMapList(supplierList);
    }
}
